package cn.kgc.springmvc.day04.handlers;

import cn.kgc.springmvc.day04.response.ResponseResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author 课工场
 * @date 2024/7/23
 * @description    全局异常处理类
 */
// @ControllerAdvice    //  标识当前类是一个全局异常处理类
// @ResponseBody
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(RuntimeException.class)
    public ResponseResult runtimeExceptionHandler(Exception e){

        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(110);
        responseResult.setMessage("删除失败");

        return  responseResult;
    }

    @ExceptionHandler(NullPointerException.class)
    public ResponseResult nullPointerExceptionHandler(Exception e){

        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(110);
        responseResult.setMessage("空指针异常");

        return  responseResult;
    }


}
